<template>
<div class="login_container">
    <!-- 登录盒子 -->
   <div class="login_box">
       <!-- 头像 -->
       <div class="avatar_box">
           <img src="../assets/logo.png" alt="">
       </div>
       <!-- 登录表单 -->
       <el-form class="login_form" :model="loginRuleForm" :rules="loginRules" ref="loginRuleForm" >
            <el-form-item prop="username">
                <el-input prefix-icon="el-icon-user" v-model="loginRuleForm.username"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" prefix-icon="el-icon-lock" v-model="loginRuleForm.password"></el-input>
            </el-form-item>
            <el-form-item class="login_btns">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="loginResetForm('loginRuleForm')">重置</el-button>
            </el-form-item>
       </el-form>
   </div>
</div>

</template>

<script>
export default {
  components: {},
  data() {
    return {
      loginRuleForm: {
        username: "admin",
        password: "123456"
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  }, // 组件方法

  methods: {
    //重置
    loginResetForm(loginRuleForm) {
      this.$refs[loginRuleForm].resetFields();
    },

    //点击登录按钮
    login() {
      // console.log('this',this);
      // console.log(this.$router);
      this.$refs.loginRuleForm.validate(async valid => {
        //如果valid为true 则表示整个表单内的校验都通过了
        if (!valid) return;
        //通过校验
        let res = await this.$http.post("/login", this.loginRuleForm);
        console.log("res", res);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
        //登录成功
        this.$message.success(res.meta.msg);
        //把token值存储到本地存储
        window.sessionStorage.setItem("token", res.data.token);
        //成功后=>跳转到首页
        this.$router.push("/home");
      });
    }
  },

  mounted() {}
};
</script> 

<style  lang='scss' scoped>
.login_container {
  position: relative;
  background-color: #2b4b6b;
  width: 100%;
  height: 100%;
  .login_box {
    box-shadow: 0 0 15px #dddddd;
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: #ffffff;
    top: 50%;
    left: 50%;
    border-radius: 3px;
    transform: translate(-50%, -50%);
    .avatar_box {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #efefef;
      border-radius: 50%;
      padding: 10px;
      width: 150px;
      height: 150px;
      background: #ffffff;
      box-shadow: 0 0 15px #dddddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #efefef;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      .login_btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>